@extends('admin.layout.layout')
@section('content')
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">用户添加</h1>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <div class="panel-body">
        <div class="row">
            <div class="col-lg-6 col-lg-offset-3">
            @if(session("error"))
                {{session('error')}}
            @endif

            @if (count($errors) > 0)
        
            <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
            </div>
            @endif
                <form action="{{url('/admin/user/insert')}}" method="post">
                    <div class="form-group">
                        <label>用户名</label>
                        <input class="form-control" type="text" name="username" value="{{old('username')}}"><span></span>
                    </div>
                    <div class="form-group">    
                        <label>密码</label>
                        <input class="form-control" type="password" name="password" value="{{old('password')}}">
                    </div>
                    <div class="form-group">
                        <label>确认密码</label>
                        <input class="form-control" type="password" name="repassword" value="{{old('repassword')}}">
                    </div>
                    <div class="form-group">
                        <label>邮箱</label>
                        <input class="form-control" type="text" name="email" value="{{old('email')}}"><span></span>
                    </div>
                    <div class="form-group">
                        <label>性别</label><br>
                        <input type="radio" name="sex" value="1" checked>男&nbsp;&nbsp;
                        <input type="radio" name="sex" value="2">女&nbsp;&nbsp;
                        <input type="radio" name="sex" value="3">保密
                    </div>
                                
                    <div class="form-group">
                        <label>手机号</label>
                        <input class="form-control" type="text" name="phone" value="{{old('phone')}}">
                    </div>
                    <div class="form-group">
                        <label>权限</label>
                        <select name="qx" class="form-control">
                            <option value="1">超级管理员</option>
                            <option value="2">店主</option>
                            <option value="3">普通会员</option>
                        </select>
                    </div>
                    
                        {{csrf_field()}}
                    <button type="submit" class="btn btn-primary btn-lg" style="margin-left:0px;width:100px">添加</button>
                    
                   
                </form>
                </div>
            </div>
        </div>

   

<script type="text/javascript" src="/public/home/js/jquery.js"></script>
    <script type="text/javascript">
    //全局变量 标识
    // alert($)
    var CUSER = false;
    var CEMAIL = false;


    //绑定表单提交事件
    $('form').submit(function(){
        //触发所有的丧失焦点事件
        $('input').trigger('blur');

        //判断所有的字段都检测成功后提交
        if(CUSER){
            //都成功
            return true;
        }

        //阻止默认行为
        // return false;
    })

    //给所有的字段绑定获取焦点事件

    //给用户名绑定丧失焦点事件
    $('input[name=username]').blur(function(){
        //获取用户名
        var uname = $(this).val();
        //声明正则验证用户名是否合法
        var reg = /^\w{6,18}$/;
        var res = reg.test(uname);
        if(!res){
            //如果用户名不符合正则要求
            $(this).next().html('用户名格式不正确').css('color','red');
            //修改input边框颜色
            $(this).css('border','1px solid red');
            //修改全局变量
            CUSER = false;
            return false;
        }
        var inp = $(this);
        $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
        });
        //发送ajax
        $.ajax({
            url:"{{url('/register/ajax')}}",
            data:{name:uname},
            type:'get',
            // dataType:'json',
            success:function(data){
                //判断返回值
                if(data == 1){
                    inp.next().html('ok').css('color','green');
                    inp.css('border','1px solid green');
                    //修改全局变量
                    CUSER = true;
                }else{
                    inp.next().html('用户名已存在,请更换').css('color','red');
                    inp.css('border','1px solid red');
                    //修改全局变量
                    CUSER = false;
                }
            },
            error:function(){
                alert('ajax错误');
            },
            // timeout:2000,
            //同步
            async:false
        })
    })
    //给邮箱绑定丧失焦点事件
   $('input[name=email]').blur(function(){
        //获取用户输入的邮箱
        var email = $(this).val();
        // alert(222);
        //声明正则
        var reg = /^\w+@\w+\.(com|cn|com\.cn|org|net)$/;
        if(reg.test(email)){
            var inpe = $(this);
        //发送ajax
        $.ajax({
            url:"{{url('/register/ajaxa')}}",
            data:{name:email},
            type:'get',
            // dataType:'json',
            success:function(data){
                // console.log(data);
                //判断返回值
                if(data == 1){
                    inpe.next().html('ok').css('color','green');
                    inpe.css('border','1px solid green');
                    //修改全局变量
                    CEMAIL = true;
                }else{
                    inpe.next().html('邮箱已注册,请更换').css('color','red');
                    inpe.css('border','1px solid red');
                    //修改全局变量
                    CEMAIL = false;
                }
            },
            error:function(){
                alert('ajax错误');
            },
            // timeout:2000,
            //同步
            async:false
        })
        }else{
            //错误
            $(this).next().html('邮箱格式不正确').css('color','red');
            $(this).css('border','1px solid red');
            CEMAIL = false;
        }


        
    })

    </script> 
@endsection 